package com.alibaba.citrus.ots.common.util;


import com.alibaba.acm.shaded.com.alibaba.metrics.StringUtils;
import com.alibaba.cz.base.tool.feature.Features;
import com.alibaba.cz.base.tool.log.Log;
import com.google.common.collect.Maps;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author hanguo
 * @date 2021/10/17
 * @Description SAP/CRM入参处理工具
 */
public class SapAndCrmFieldDealUtils {

    private static Log log = Log.getLogger(SapAndCrmFieldDealUtils.class);

    /**
     * sap请求数据id字段 前置0去掉
     *
     * @param s
     * @return
     */
    public static String generateIdField(String s) {
        if (StringUtils.isBlank(s)){
            return null;
        }
        String[] split = s.split("");
        String substring = null;
        for (int i = 0; i < split.length; i++) {
            if (!"0".equals(split[i])) {
                substring = s.substring(i);
                break;
            }
        }
        return substring;
    }
    /**
     * 截取scItemCode 固定长度为12
     * @param str
     * @return
     */
    public static String subStringScItmCode(String str){
        if (StringUtils.isBlank(str)){
            return null;
        }
        if (str.length()>12){
            return str.substring(str.length()-12);
        }
        return str;
    }
    /**
     * 截取后12位
     * @param s
     * @return
     */
    public static String generateScItemIdField(String s) {
        if (StringUtils.isBlank(s)){
            return null;
        }
        String substring = "";
        if (s.length() >= 12){
            substring = s.substring(s.length()-12);
        }else {
            DecimalFormat g1=new DecimalFormat("000000000000");
            substring = g1.format(Integer.valueOf(s));
        }
        return substring;
    }

    /**
     * 字符串转Date适用于yyyyMMdd或者yyyyMMddHHmmss格式字符串转为Date
     *
     * @param date
     * @return
     */
    public static Date toDate(String date) {
        if (StringUtils.isBlank(date)|| Long.valueOf(date).toString().length() < 8) {
            return null;
        }

        String dateStr = null;
        Date result = null;
        //传的是yyyyMMddHHmmss格式数据
        if (date.length() > 8) {
            dateStr = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8) + " " + date.substring(8, 10) + ":" + date.substring(10, 12) + ":" + date.substring(12, 14);
        }
        if (date.length() == 8) {
            dateStr = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8) + " 00:00:00";
        }
        if (date.length() < 8){
            return null;
        }

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            result = sdf.parse(dateStr);
        } catch (Exception e) {
            log.error("时间格式错误");
            new ParseException(e.getMessage(), 0);
        }


        return result;
    }

    /**
     * 将pattern格式字符串转为Date
     */

    public static Date toEndDate(String date)  {
        if (StringUtils.isBlank(date)|| Long.valueOf(date).toString().length() < 8) {
            return null;
        }

        String dateStr = null;
        Date result = null;
        //传的是yyyyMMddHHmmss格式数据
        if (date.length() >= 8) {
            dateStr = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8) + " 23:59:59";
        }
        if (date.length() < 8){
            return null;
        }

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            result = sdf.parse(dateStr);
        } catch (Exception e) {
            log.error("时间格式错误");
            new ParseException(e.getMessage(), 0);
        }


        return result;


    }

    /**
     * 用于价格的开始时间转换
     */
    public static Date forPriceStartDate(String date) throws ParseException {
        if (StringUtils.isBlank(date)) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

           return simpleDateFormat.parse(date);
    }

    /**
     * 用于价格的开始时间转换
     */
    public static Date forPriceEndDate(String date) throws ParseException {
        if (StringUtils.isBlank(date)) {
            return null;
        }
        date = date +" 23:59:59";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

           return simpleDateFormat.parse(date);
    }


    /**
     * 字符串格式化
     *
     * @param date
     * @return
     */
    public static String toDateString(String date) {
        if (StringUtils.isBlank(date)) {
            return null;
        }

        String dateStr = null;
        //传的是yyyyMMddHHmmss格式数据
        if (date.length() > 8) {
            dateStr = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8) + " " + date.substring(8, 10) + ":" + date.substring(10, 12) + ":" + date.substring(12, 14);
        }
        if (date.length() == 8) {
            dateStr = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8) + " 00:00:00";
        }
        if(date.length() == 6){
            dateStr = date.substring(0, 2) + ":" + date.substring(2, 4) + ":" + date.substring(4, 6);
        }
//        else {
//            dateStr = date.substring(0, 2) + ":" + date.substring(2, 4) + ":" + date.substring(4, 6);
//        }
        return dateStr;
    }


    /**
     * 金额、数量转Integer
     */

    public static Integer toInteger(String quantityOrAmount) {

        if (StringUtils.isNotBlank(quantityOrAmount)) {

            quantityOrAmount = quantityOrAmount.trim();
            if(quantityOrAmount.contains("-")){
                quantityOrAmount=quantityOrAmount.replace("-","");
            }
            if (StringUtils.isNotBlank(quantityOrAmount)) {
                return Integer.parseInt(quantityOrAmount.split("\\.")[0]);
            }

        }

        return null;
    }

    /**
     * 金额、数量转Long
     */

    public static Long toLong(String quantityOrAmount) {
        if (StringUtils.isNotBlank(quantityOrAmount)) {
            quantityOrAmount = quantityOrAmount.trim();
            if (StringUtils.isNotBlank(quantityOrAmount)) {
                return Long.parseLong(quantityOrAmount.split("\\.")[0]);
            }
        }

        return null;
    }

    public static Long toLongByPrice(String price) {
        if (StringUtils.isNotBlank(price)) {
            BigDecimal b = new BigDecimal(price);
            return b.multiply(new BigDecimal(100)).longValue();
        }

        return null;
    }

    /**
     * 元转为分
     */
    public static Long toLong2(String sAmount){
        if (StringUtils.isNotBlank(sAmount)) {
            sAmount = sAmount.trim();
            BigDecimal amount = new BigDecimal(sAmount);
            BigDecimal multiply = new BigDecimal(100);
            return amount.multiply(multiply).longValue();
        }
        return null;
    }
    /**
     * 将字符串转换为map
     * @param featureStr
     * @return
     */
    public static Map<String, String> String2FeatureMap(String featureStr) {
        if (StringUtils.isBlank(featureStr)) {
            return Maps.newHashMap();
        }
        Features features = Features.newFeatures(featureStr);
        return features.getOriginMap();
    }

    /**
     * 字符串去空字符
     * @param str
     * @return
     */
    public static String trimStr(String str){
        if(StringUtils.isNotBlank(str)){
            return str.trim();
        }
        return null;
    }


}
